package com.kld.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.kld.common.core.domain.entity.SysDept;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kld.system.domain.SysSyncTime;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.system.domain.SysDeptMdm;
import com.kld.system.mapper.SysDeptMdmMapper;
import com.kld.system.service.ISysDeptMdmService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 机构Service业务层处理
 *
 * @author kld
 * @date 2022-06-16
 */
@RequiredArgsConstructor
@Service
public class SysDeptMdmServiceImpl implements ISysDeptMdmService {

    private final SysDeptMdmMapper baseMapper;

    /**
     * 查询机构
     *
     * @param id 机构主键
     * @return 机构
     */
    @Override
    public SysDeptMdm queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询机构列表
     *
     * @param bo 机构
     * @return 机构
     */
    @Override
    public TableDataInfo<SysDeptMdm> queryPageList(SysDeptMdm bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysDeptMdm> lqw = buildQueryWrapper(bo);
        Page<SysDeptMdm> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询部门管理数据
     *
     * @return 部门信息集合
     */
    @Override
    @DS("slave")
    public List<SysDeptMdm> selectDeptMdmList(SysSyncTime syncTime) {
        Date time = null;
        if(syncTime != null ){
            time =  syncTime.getSyncTime();
        }
        SysDeptMdm sysDeptMdm = new SysDeptMdm();
        sysDeptMdm.setUpdateTime(time);
        return baseMapper.selectDeptMdmList(sysDeptMdm);
    }

    private LambdaQueryWrapper<SysDeptMdm> buildQueryWrapper(SysDeptMdm bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysDeptMdm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), SysDeptMdm::getOrgId, bo.getOrgId());
        lqw.eq(bo.getAppId() != null, SysDeptMdm::getAppId, bo.getAppId());
        lqw.like(StringUtils.isNotBlank(bo.getOrgName()), SysDeptMdm::getOrgName, bo.getOrgName());
        lqw.eq(bo.getOrgType() != null, SysDeptMdm::getOrgType, bo.getOrgType());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgCode()), SysDeptMdm::getOrgCode, bo.getOrgCode());
        lqw.eq(bo.getParentId() != null, SysDeptMdm::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getParentIds()), SysDeptMdm::getParentIds, bo.getParentIds());
        lqw.eq(bo.getStatus() != null, SysDeptMdm::getStatus, bo.getStatus());
        lqw.eq(bo.getSort() != null, SysDeptMdm::getSort, bo.getSort());
        lqw.eq(bo.getDel() != null, SysDeptMdm::getDel, bo.getDel());
        lqw.eq(bo.getCreator() != null, SysDeptMdm::getCreator, bo.getCreator());
        lqw.eq(bo.getUpdator() != null, SysDeptMdm::getUpdator, bo.getUpdator());
        lqw.eq(StringUtils.isNotBlank(bo.getOuterId()), SysDeptMdm::getOuterId, bo.getOuterId());
        lqw.eq(StringUtils.isNotBlank(bo.getExtend1()), SysDeptMdm::getExtend1, bo.getExtend1());
        lqw.eq(StringUtils.isNotBlank(bo.getExtend2()), SysDeptMdm::getExtend2, bo.getExtend2());
        lqw.eq(StringUtils.isNotBlank(bo.getExtend3()), SysDeptMdm::getExtend3, bo.getExtend3());
        lqw.eq(StringUtils.isNotBlank(bo.getExtend4()), SysDeptMdm::getExtend4, bo.getExtend4());
        lqw.eq(StringUtils.isNotBlank(bo.getExtend5()), SysDeptMdm::getExtend5, bo.getExtend5());
        lqw.eq(bo.getExtend6() != null, SysDeptMdm::getExtend6, bo.getExtend6());
        lqw.eq(bo.getExtend7() != null, SysDeptMdm::getExtend7, bo.getExtend7());
        lqw.eq(bo.getExtend8() != null, SysDeptMdm::getExtend8, bo.getExtend8());
        return lqw;
    }
}
